<?php

namespace addons\tgbot;

use app\common\library\Menu;
use think\Addons;
use TelegramBot\Api\BotApi;

/**
 * 插件
 */
class Tgbot extends Addons
{
    /**
     * 插件安装方法
     * @return bool
     */
    public function install()
    {
        
        return true;
    }

    /**
     * 插件卸载方法
     * @return bool
     */
    public function uninstall()
    {
        
        return true;
    }

    /**
     * 插件启用方法
     * @return bool
     */
    public function enable()
    {
        
        return true;
    }

    /**
     * 插件禁用方法
     * @return bool
     */
    public function disable()
    {
        
        return true;
    }

    public function run(){

    }

    /**
     * 应用初始化
     */
    public function appInit()
    {
        \think\Loader::addNamespace('TelegramBot\Api', ADDON_PATH . 'tgbot' . DS . 'library' . DS . 'TelegramBot' . DS . 'Api' . DS);
    }

    /**
     * 上传配置
     */
    public function responseSend(){
        $config = get_addon_config('tgbot');
        if(!empty($config['robottokens'])){
            $tokens = explode("\r\n",$config['robottokens']);
            foreach($tokens as $k => $v){
                $bot = new \TelegramBot\Api\BotApi($v);
                if(!empty($config['callbackUrl'])){
                    $bot->setWebhook($config['callbackUrl']);
                }else{
                    $bot->setWebhook('https://' . $_SERVER['HTTP_HOST'] . '/index/tgbot/receive');
                }
                
            }
        }
    }

}
